package com.daoImpl;

import java.util.List;

import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

import com.dao.*;
import com.entity.Device;

public class DeviceDaoImpl extends HibernateDaoSupport implements DeviceDao  {

	@Override
	public List<Device> findAllDevice() {
		List list=(List<Device>)this.getHibernateTemplate().find("from Device");
        return list;
	}

	@Override
	public List<Device> findDeviceBySubstation(String substationNo) {
		List list = (List<Device>) this.getHibernateTemplate().find("from Device a where a.substationNo=?",new Object[]{substationNo});
        return list;
	}

	@Override
	public List<Device> findDeviceByModel(String deviceModel) {
		List list = (List<Device>) this.getHibernateTemplate().find("from Device a where a.deviceModel=?",new Object[]{deviceModel});
        return list;
	}

	@Override
	public List<Device> findDeviceByNorm(String deviceNorm) {
		List list = (List<Device>) this.getHibernateTemplate().find("from Device a where a.deviceNorm=?",new Object[]{deviceNorm});
        return list;
	}

	@Override
	public List<Device> findDeviceByTypeAndNorm(String deviceType,String deviceNorm) {
		List list = (List<Device>) this.getHibernateTemplate().find("from Device a where a.deviceType=? and a.deviceNorm=?",new Object[]{deviceType,deviceNorm});
        return list;
	}

	@Override
	public Device FindDeviceByID(int deviceID) {
		return (Device) this.getHibernateTemplate().get(Device.class, deviceID);
	}
	
	@Override
	public Device FindDeviceByNo(String deviceNo) {
		List list = (List<Device>) this.getHibernateTemplate().find("from Device a where a.deviceNo=?",new Object[]{deviceNo});
        Device device = (Device)list.get(0);
		return device;
	}

	@Override
	public boolean addDevice(Device device) {
		try{
            this.getHibernateTemplate().clear();
            this.getHibernateTemplate().save(device);
            return true;
        }catch (Exception ex){
            ex.printStackTrace();
            return false;
        }
	}

	@Override
	public boolean deleteDevice(Device device) {
		try{
            this.getHibernateTemplate().delete(device);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
	}

	@Override
	public boolean updateDevice(Device device) {
		try{
            this.getHibernateTemplate().clear();
            this.getHibernateTemplate().update(device);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
	}

	@Override
	public List<Device> findDeviceByNorm(String deviceType, String deviceNorm) {
		List list = (List<Device>) this.getHibernateTemplate().find("from Device a where a.deviceType=? and a.deviceNorm=?",new Object[]{deviceType,deviceNorm});
		return list;
	}

	@Override
	public List<Device> findDeviceByUnit(String unitNo,String substationNo){
		String hql = "from Device where deviceNo = (select deviceNo from UnitInterval where unitNo = '"+unitNo+"' and substationNo = '"+substationNo+"')";
		List list = (List<Device>) this.getHibernateTemplate().find(hql);
		return list;
	}
	
	@Override
	public List<Device> findAuditingDevice(){
		String hql = "from Device d where d.deviceStatus = 1 or d.deviceStatus = 3 or d.deviceStatus = 5 or d.deviceStatus = 7 ";
		List list = (List<Device>) this.getHibernateTemplate().find(hql);
		return list;
	}

	@Override
	public boolean checkDeviceNoAvailable(String deviceNo) {
		List list = (List<Device>) this.getHibernateTemplate().find("from Device a where a.deviceNo=?",new Object[]{deviceNo});
        if(list.get(0) != null)
        	return true;
        else return false;
	}

	@Override
	public List<Device> findDeviceNotInUnitInterval() {
		String hql = "from Device d where d.deviceNo not in (select deviceNo from UnitInterval)";
		List list = (List<Device>) this.getHibernateTemplate().find(hql);
		return list;
	}

	@Override
	public List<Device> findDeviceBySubstationNotInUnitInterval(String substationNo) {
		String hql = "from Device d where d.deviceStatus != 8 and d.deviceStatus != 7 and d.substationNo = '"+substationNo+"' and d.deviceNo not in (select deviceNo from UnitInterval)";
		List list = (List<Device>) this.getHibernateTemplate().find(hql);
		return list;
	}
	
	@Override
	public List<Device> deviceDetails(String unitNo,String substationNo){
		String hql = "from Device d where d.deviceNo in (select deviceNo from UnitInterval u where u.unitNo = '"+ unitNo +"' and u.substationNo = '"+ substationNo +"')";
		List list = (List<Device>) this.getHibernateTemplate().find(hql);
		return list;
	}
	
	@Override
	public void deleteDevice(int deviceId){
		String hql = "from Device d where d.deviceId = '"+ deviceId +"')";
		List list = (List<Device>) this.getHibernateTemplate().find(hql);
		this.getHibernateTemplate().delete(list.get(0));
		
	}

}
